import re
import time


def rule_add(session_page, unit):
    session_page.get_by_test_id("processing-1002").click()  # 转入定制规则
    session_page.get_by_test_id("processing-2001").click()  # 点击新增按钮
    session_page.get_by_test_id("processing-3001").get_by_text("富通东方").click()  # 填写应用客户
    session_page.get_by_text("应用客户").click()
    session_page.get_by_test_id("processing-3002").get_by_placeholder("请输入").fill(unit["rule_name"])  # 填写规则名称
    session_page.get_by_test_id("processing-3003").get_by_placeholder("请输入").fill(
        unit["rule_description"])  # 填写规则描述

    # 选择处理阶段
    if unit["rule_processing_stage"] == "前置规则":
        session_page.get_by_test_id("processing-3004").get_by_text("前置规则").click()

        if unit["rule_format"] == "文本":
            session_page.get_by_test_id("processing-3006").get_by_text("文本").click()
        else:
            session_page.get_by_test_id("processing-3006").get_by_text("工单").click()

        list0_data_channels = unit["rule_data_channels"].split(",")

        # 选择数据渠道
        if len(list0_data_channels) == 1:
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menuitem", name=unit["rule_data_channels"]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 2:
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[1]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 3:
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[1]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[2]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()

        # 选择条件限制
        if unit["rule_conditional_limitations"] == "满足全部条件":
            session_page.get_by_text("满足全部条件(AND)").click()
        else:
            session_page.get_by_text("满足任意条件(OR)").click()

        # 选择规则条件
        # 字段
        session_page.get_by_test_id("sql-1-9001-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_field"])

        # 变量值
        session_page.get_by_test_id("sql-1-9002-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_value"])

        # 逻辑运算符
        session_page.get_by_test_id("sql-1-9003-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list',
                            unit["rule_conditions"][0]["condition_operator"])

        # 条件类型
        if unit["rule_conditions"][0]["condition_operator"] == '为空' or unit["rule_conditions"][0][
            "condition_operator"] == '不为空':
            pass
        else:
            session_page.get_by_test_id("sql-1-9004-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_conditions"][0]["condition_type"])

            # 条件详情
            session_page.get_by_test_id("sql-1-9005-0").click()
            if unit["rule_conditions"][0]["condition_type"] == '值' or unit["rule_conditions"][0][
                "condition_type"] == '正则表达式':
                session_page.get_by_placeholder("条件详情").fill(unit["rule_conditions"][0]["condition_details"])
            else:
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_conditions"][0]["condition_details"])

    # 后置规则
    else:
        session_page.get_by_test_id("processing-3004").get_by_text("后置规则").click()

        # 选择规则类型
        if unit["rule_type"] == "修改":
            session_page.get_by_test_id("processing-3005").get_by_text("修改").click()
        if unit["rule_type"] == "脱敏":
            session_page.get_by_test_id("processing-3005").get_by_text("脱敏").click()
        if unit["rule_type"] == "补充":
            session_page.get_by_test_id("processing-3005").get_by_text("补充").click()
        if unit["rule_type"] == "清洗":
            session_page.get_by_test_id("processing-3005").get_by_text("清洗").click()
        if unit["rule_type"] == "过滤":
            session_page.get_by_test_id("processing-3005").get_by_text("过滤").click()

        # 选择内容格式
        if unit["rule_format"] == "文本":
            session_page.get_by_test_id("processing-3006").get_by_text("文本").click()
        else:
            session_page.get_by_test_id("processing-3006").get_by_text("工单").click()

        # 选择数据渠道
        list0_data_channels = unit["rule_data_channels"].split(",")

        if len(list0_data_channels) == 1:
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menuitem", name=unit["rule_data_channels"]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 2:
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[1]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 3:
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[1]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[2]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()

        # 选择条件限制
        if unit["rule_conditional_limitations"] == "满足全部条件":
            session_page.get_by_text("满足全部条件(AND)").click()
        else:
            session_page.get_by_text("满足任意条件(OR)").click()

        # 选择规则条件
        # for i in range(len(unit["rule_conditions"])):
        # 字段
        session_page.get_by_test_id("sql-1-9001-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_field"])

        # 变量值
        session_page.get_by_test_id("sql-1-9002-0").get_by_placeholder("变量值").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_value"])

        # 逻辑运算符
        session_page.get_by_test_id("sql-1-9003-0").get_by_placeholder("逻辑运算符").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list',
                            unit["rule_conditions"][0]["condition_operator"])

        # 条件类型
        if unit["rule_conditions"][0]["condition_operator"] == '为空' or unit["rule_conditions"][0][
            "condition_operator"] == '不为空':
            pass
        else:
            session_page.get_by_test_id("sql-1-9004-0").get_by_placeholder("条件类型").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_conditions"][0]["condition_type"])

            # 条件详情
            session_page.get_by_test_id("sql-1-9005-0").get_by_placeholder("条件详情").click()
            if unit["rule_conditions"][0]["condition_type"] == '值' or unit["rule_conditions"][0][
                "condition_type"] == '正则表达式':
                session_page.get_by_test_id("sql-1-9005-0").get_by_placeholder("条件详情").fill(
                    unit["rule_conditions"][0]["condition_details"])
            else:
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_conditions"][0]["condition_details"])

            # session_page.get_by_test_id("sql-1-9007").click()

        # 选择动作
        # 字段
        if unit["rule_type"] == '过滤':
            pass
        else:
            session_page.get_by_test_id("sql-2-9001-0").get_by_placeholder("字段").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_field"])

            # 变量值
            session_page.get_by_test_id("sql-2-9002-0").get_by_placeholder("变量值").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_value"])

            # 逻辑运算符
            session_page.get_by_test_id("sql-2-9003-0").get_by_placeholder("逻辑运算符").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_operator"])

            # 条件类型
            session_page.get_by_test_id("sql-2-9004-0").get_by_placeholder("条件类型").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_type"])

            # 条件详情
            session_page.get_by_test_id("sql-2-9005-0").click()
            if unit["rule_action"][0]["action_type"] == '值' or unit["rule_action"][0][
                "action_type"] == '正则表达式':
                session_page.get_by_test_id("sql-2-9005-0").get_by_placeholder("条件详情").fill(
                    unit["rule_action"][0]["action_details"])
            else:
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_action"][0]["action_details"])

        # 选择权重
        session_page.get_by_test_id("processing-3010").click()

        if unit["rule_weight"] == 'A':
            session_page.locator("li").filter(has_text=re.compile(r"^A$")).click()
        if unit["rule_weight"] == 'B':
            session_page.locator("li").filter(has_text=re.compile(r"^B$")).click()
        if unit["rule_weight"] == 'C':
            session_page.locator("li").filter(has_text=re.compile(r"^C$")).click()
        if unit["rule_weight"] == 'D':
            session_page.locator("li").filter(has_text=re.compile(r"^D$")).click()
        if unit["rule_weight"] == 'E':
            session_page.locator("li").filter(has_text=re.compile(r"^E$")).click()

    # 点击确定
    session_page.get_by_role("button", name="确定").click()

    time.sleep(0.5)

    if session_page.get_by_role("alert").is_visible():
        raise Exception("新增规则信息失败")
    else:
        assert unit["rule_name"] == session_page.get_by_test_id("processing-custom-2001-t0-0").text_content()


# 下拉列表取值方法
def get_dropdown_values(session_page, selector_id, selector_text):
    dropdown = session_page.locator(selector_id)
    # print(dropdown.all())
    for item in dropdown.all():
        if item.is_visible():  # 检查元素是否可见
            # 获取当前项下所有的 li 元素
            list_items = item.locator('li').all()
            for list_item in list_items:
                # 获取每个 li 元素的文本
                text = list_item.text_content()
                if selector_text in text:  # 检查文本是否包含 '四级标签/话题'
                    list_item.click()  # 点击该 li 元素
                    break


# 编辑方法
def rule_eidt(session_page, unit):
    session_page.get_by_test_id("processing-1002").click()  # 转入定制规则
    session_page.locator("td:nth-child(12) > .arco-table-cell").first.get_by_test_id(
        "processing-2003").click()  # 点击第二行的编辑按钮
    # session_page.get_by_role("row").get_by_test_id("processing-2003").click()
    session_page.get_by_test_id("processing-3002").get_by_placeholder("请输入").fill(unit["rule_name"])  # 填写规则名称
    session_page.get_by_test_id("processing-3003").get_by_placeholder("请输入").fill(
        unit["rule_description"])  # 填写规则描述
    #
    # 选择处理阶段
    if unit["rule_processing_stage"] == "前置规则":
        session_page.get_by_test_id("processing-3004").get_by_text("前置规则").click()

        if unit["rule_format"] == "文本":
            session_page.get_by_test_id("processing-3006").get_by_text("文本").click()
        else:
            session_page.get_by_test_id("processing-3006").get_by_text("工单").click()

        # 选择数据渠道
        list0_data_channels = unit["rule_data_channels"].split(",")
        if len(list0_data_channels) == 1:
            session_page.get_by_test_id("processing-3007").locator("span").nth(1).click()
            session_page.get_by_test_id("processing-3007").locator("path").nth(1).click()
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menuitem", name=unit["rule_data_channels"]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 2:
            session_page.get_by_test_id("processing-3007").locator("span").nth(1).click()
            session_page.get_by_test_id("processing-3007").locator("path").nth(1).click()
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[1]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 3:
            session_page.get_by_test_id("processing-3007").locator("span").nth(1).click()
            session_page.get_by_test_id("processing-3007").locator("path").nth(1).click()
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[1]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[2]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()

        # 选择条件限制
        if unit["rule_conditional_limitations"] == "满足全部条件":
            session_page.get_by_text("满足全部条件(AND)").click()
        else:
            session_page.get_by_text("满足任意条件(OR)").click()

        # 选择规则条件
        session_page.get_by_test_id("sql-1-9001-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_field"])

        session_page.get_by_test_id("sql-1-9002-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_value"])

        session_page.get_by_test_id("sql-1-9003-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list',
                            unit["rule_conditions"][0]["condition_operator"])

        if unit["rule_conditions"][0]["condition_operator"] == '为空' or unit["rule_conditions"][0][
            "condition_operator"] == '不为空':
            pass
        else:
            session_page.get_by_test_id("sql-1-9004-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_conditions"][0]["condition_type"])

            session_page.get_by_test_id("sql-1-9005-0").click()
            if unit["rule_conditions"][0]["condition_type"] == '值' or unit["rule_conditions"][0][
                "condition_type"] == '正则表达式':
                session_page.get_by_test_id("sql-1-9005-0").locator('input[type="text"]').fill(
                    unit["rule_conditions"][0]["condition_details"])
            else:
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_conditions"][0]["condition_details"])

    # 后置规则
    else:
        session_page.get_by_test_id("processing-3004").get_by_text("后置规则").click()

        # 选择规则类型
        if unit["rule_type"] == "修改":
            session_page.get_by_test_id("processing-3005").get_by_text("修改").click()
        if unit["rule_type"] == "脱敏":
            session_page.get_by_test_id("processing-3005").get_by_text("脱敏").click()
        if unit["rule_type"] == "补充":
            session_page.get_by_test_id("processing-3005").get_by_text("补充").click()
        if unit["rule_type"] == "清洗":
            session_page.get_by_test_id("processing-3005").get_by_text("清洗").click()
        if unit["rule_type"] == "过滤":
            session_page.get_by_test_id("processing-3005").get_by_text("过滤").click()

        # 选择内容格式
        if unit["rule_format"] == "文本":
            session_page.get_by_test_id("processing-3006").get_by_text("文本").click()
        else:
            session_page.get_by_test_id("processing-3006").get_by_text("工单").click()

        # 选择数据渠道
        list0_data_channels = unit["rule_data_channels"].split(",")

        if len(list0_data_channels) == 1:
            session_page.get_by_test_id("processing-3007").locator("span").nth(1).click()
            session_page.get_by_test_id("processing-3007").locator("path").nth(1).click()
            # session_page.get_by_test_id("processing-3007").get_by_label("Close").click()
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menuitem", name=unit["rule_data_channels"]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 2:
            session_page.get_by_test_id("processing-3007").locator("span").nth(1).click()
            session_page.get_by_test_id("processing-3007").locator("path").nth(1).click()
            # session_page.get_by_test_id("processing-3007").get_by_label("Close").click()
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[1]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()
        if len(list0_data_channels) == 3:
            session_page.get_by_test_id("processing-3007").locator("span").nth(1).click()
            session_page.get_by_test_id("processing-3007").locator("path").nth(1).click()
            # session_page.get_by_test_id("processing-3007").get_by_label("Close").click()
            session_page.get_by_test_id("processing-3007").get_by_placeholder("全部").click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[0]).click()
            session_page.get_by_role("menu").get_by_text(list0_data_channels[1]).click()
            session_page.get_by_role("menuitem", name=list0_data_channels[2]).locator("span div").click()
            session_page.get_by_test_id("processing-3011").get_by_text("数据渠道").click()

        # 选择条件限制
        if unit["rule_conditional_limitations"] == "满足全部条件":
            session_page.get_by_text("满足全部条件(AND)").click()
        else:
            session_page.get_by_text("满足任意条件(OR)").click()

        # 选择规则条件
        # 字段
        session_page.get_by_test_id("sql-1-9001-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_field"])

        # 变量值
        session_page.get_by_test_id("sql-1-9002-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][0]["condition_value"])

        # 逻辑运算符
        session_page.get_by_test_id("sql-1-9003-0").click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list',
                            unit["rule_conditions"][0]["condition_operator"])

        # 条件类型
        if unit["rule_conditions"][0]["condition_operator"] == '为空' or unit["rule_conditions"][0][
            "condition_operator"] == '不为空':
            pass
        else:
            session_page.get_by_test_id("sql-1-9004-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_conditions"][0]["condition_type"])

            # 条件详情
            if unit["rule_conditions"][0]["condition_type"] == '值' or unit["rule_conditions"][0][
                "condition_type"] == '正则表达式':
                session_page.get_by_test_id("sql-1-9005-0").locator('input[type="text"]').fill(
                    unit["rule_conditions"][0]["condition_details"])
            else:
                session_page.get_by_test_id("sql-1-9005-0").click()
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_conditions"][0]["condition_details"])

        # 选择动作
        # 字段
        if unit["rule_type"] == '过滤':
            pass
        else:
            session_page.get_by_test_id("sql-2-9001-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_field"])

            # 变量值
            session_page.get_by_test_id("sql-2-9002-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_value"])

            # 逻辑运算符
            session_page.get_by_test_id("sql-2-9003-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_operator"])

            # 条件类型
            session_page.get_by_test_id("sql-2-9004-0").click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_action"][0]["action_type"])

            # 条件详情
            session_page.get_by_test_id("sql-2-9005-0").click()
            if unit["rule_action"][0]["action_type"] == '值' or unit["rule_action"][0][
                "action_type"] == '正则表达式':
                session_page.get_by_test_id("sql-2-9005-0").locator('input[type="text"]').fill(
                    unit["rule_action"][0]["action_details"])
            else:
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_action"][0]["action_details"])

    # 选择权重
    session_page.get_by_test_id("processing-3010").click()

    if unit["rule_weight"] == 'A':
        session_page.locator("li").filter(has_text=re.compile(r"^A$")).click()
    if unit["rule_weight"] == 'B':
        session_page.locator("li").filter(has_text=re.compile(r"^B$")).click()
    if unit["rule_weight"] == 'C':
        session_page.locator("li").filter(has_text=re.compile(r"^C$")).click()
    if unit["rule_weight"] == 'D':
        session_page.locator("li").filter(has_text=re.compile(r"^D$")).click()
    if unit["rule_weight"] == 'E':
        session_page.locator("li").filter(has_text=re.compile(r"^E$")).click()

    # 点击确定
    session_page.get_by_role("button", name="确定").click()

    if session_page.get_by_role("alert").is_visible():
        raise Exception("编辑规则信息失败")
    else:
        assert unit["rule_name"] == session_page.get_by_test_id("processing-custom-2001-t0-0").text_content()

    session_page.reload()


# 规则验证
def rule_verification(session_page, unit):
    session_page.get_by_test_id("processing-1002").click()  # 转入定制规则
    session_page.locator("tr:nth-child(2) > td:nth-child(12) > .arco-table-cell").first.get_by_test_id(
        "processing-2006").click()
    time.sleep(0.5)
    rule_name = session_page.get_by_test_id("processing-checkandtest-1001").inner_text()

    session_page.get_by_placeholder("开始日期").click()
    session_page.get_by_test_id("processing-4001").get_by_placeholder("开始日期").fill(unit["begin_date"])
    session_page.get_by_placeholder("结束日期").click()
    session_page.get_by_test_id("processing-4001").get_by_placeholder("结束日期").fill(unit["end_date"])
    session_page.get_by_test_id("processing-4001").get_by_placeholder("结束日期").press("Enter")

    if unit["rule_format"] == "文本":
        session_page.get_by_test_id("processing-4002").get_by_text("文本").click()
    else:
        session_page.get_by_test_id("processing-4002").get_by_text("工单").click()

    list1_data_channels = unit["rule_data_channels"].split(",")

    # 选择数据渠道
    if len(list1_data_channels) == 1:
        session_page.get_by_test_id("processing-4003").click()
        session_page.get_by_test_id("processing-4003").locator("path").nth(1).click()
        session_page.get_by_role("menuitem", name=unit["rule_data_channels"]).locator("span div").click()
        session_page.get_by_test_id("processing-4006").get_by_text("内容格式", exact=True).click()
    if len(list1_data_channels) == 2:
        session_page.get_by_test_id("processing-4003").click()
        session_page.get_by_test_id("processing-4003").locator("path").nth(1).click()
        session_page.get_by_role("menu").get_by_text(list1_data_channels[0]).click()
        session_page.get_by_role("menuitem", name=list1_data_channels[1]).locator("span div").click()
        session_page.get_by_test_id("processing-4003").get_by_text("内容格式", exact=True).click()
    if len(list1_data_channels) == 3:
        session_page.get_by_test_id("processing-4003").click()
        session_page.get_by_test_id("processing-4003").locator("path").nth(1).click()
        session_page.get_by_role("menu").get_by_text(list1_data_channels[0]).click()
        session_page.get_by_role("menu").get_by_text(list1_data_channels[1]).click()
        session_page.get_by_role("menuitem", name=list1_data_channels[2]).locator("span div").click()
        session_page.get_by_test_id("processing-4006").get_by_text("内容格式", exact=True).click()

    if unit["rule_conditional_limitations"] == "满足全部条件":
        session_page.get_by_test_id("processing-4004").get_by_text("满足全部条件(AND)").click()
    else:
        session_page.get_by_test_id("processing-4004").get_by_text("满足任意条件(OR)").click()

    # 选择规则条件
    # 字段
    for i in range(len(unit["rule_conditions"])):

        session_page.get_by_test_id("sql-1-9001-" + str(i)).click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][i]["condition_field"])

        # 变量值
        session_page.get_by_test_id("sql-1-9002-" + str(i)).click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][i]["condition_value"])

        # 逻辑运算符
        session_page.get_by_test_id("sql-1-9003-" + str(i)).click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list',
                            unit["rule_conditions"][i]["condition_operator"])

        # 条件类型
        if (unit["rule_conditions"][i]["condition_operator"] == '为空' or unit["rule_conditions"][i][
            "condition_operator"] ==
                '不为空'):
            pass
        else:
            session_page.get_by_test_id("sql-1-9004-" + str(i)).click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_conditions"][i]["condition_type"])

            # 条件详情
            if (unit["rule_conditions"][i]["condition_type"] == '值' or unit["rule_conditions"][i]["condition_type"] ==
                    '正则表达式'):
                session_page.get_by_test_id("sql-1-9005-" + str(i)).locator('input[type="text"]').fill(
                    unit["rule_conditions"][i]["condition_details"])
            else:
                session_page.get_by_test_id("sql-1-9005-" + str(i)).click()
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_conditions"][0]["condition_details"])

        if i < len(unit["rule_conditions"]) - 1:
            session_page.get_by_test_id("sql-1-9007").click()
        else:
            break

    session_page.get_by_test_id("processing-4005").click()
    time.sleep(0.5)

    session_page.get_by_role("button", name="确定").click()
    time.sleep(0.5)
    # session_page.reload()

    session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").click()
    session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").fill(rule_name)
    session_page.get_by_test_id("processing-custom-1008").click()

    test_str = session_page.get_by_test_id("processing-custom-2001-t8-0").inner_text()
    print("【test_str】", test_str)
    if test_str == '校验中':
        time.sleep(80)
        session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").click()
        session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").fill(rule_name)
        session_page.get_by_test_id("processing-custom-1008").click()
        test_str1 = session_page.get_by_test_id("processing-custom-2001-t8-0").inner_text()
        print("【test_str1】", test_str1)

        if test_str1 == '校验成功':
            print("校验成功")
            session_page.get_by_test_id("processing-2009-0").click()
            test_str2 = session_page.locator("td:nth-child(6) > .arco-table-cell > .arco-auto-tooltip").first.inner_text()
            print("【test_str2】", test_str2)
            if test_str2 is not None:
                print("验证成功")
                session_page.mouse.move(0, 0)
                # 在当前位置执行点击操作
                session_page.mouse.click(x=0, y=0)
            elif test_str2 is None:
                raise Exception("验证失败:test_str2为空或未找到")
        else:
            raise Exception("验证失败：test_str1为空或未找到")
            session_page.mouse.move(0, 0)
            # 在当前位置执行点击操作
            session_page.mouse.click(x=0, y=0)
    elif test_str == '校验成功':
        session_page.get_by_test_id("processing-2009-0").click()
        test_str3 = session_page.locator("td:nth-child(6) > .arco-table-cell > .arco-auto-tooltip").first.inner_text()
        if test_str3 is not None:
            print("验证成功")
            session_page.mouse.move(0, 0)
            # 在当前位置执行点击操作
            session_page.mouse.click(x=0, y=0)
        elif test_str3 is None:
            raise Exception("验证失败：test_str3为空或未找到")
    elif test_str == '校验失败':
        raise Exception('验证失败：test_str为空或未找到')

    session_page.reload()


def rule_testing(session_page, unit):
    session_page.get_by_test_id("processing-1002").click()  # 转入定制规则
    # stat_str = session_page.locator("tr:nth-child(2) > td:nth-child(12) > .arco-table-cell").inner_text()
    # if stat_str == '启用':
    session_page.locator("tr:nth-child(3) > td:nth-child(12) > .arco-table-cell").first.get_by_test_id(
        "processing-2007").click()
    time.sleep(0.5)
    rule_name = session_page.get_by_test_id("processing-checkandtest-1001").inner_text()

    session_page.get_by_placeholder("开始日期").click()
    session_page.get_by_test_id("processing-4001").get_by_placeholder("开始日期").fill(unit["begin_date"])
    session_page.get_by_placeholder("结束日期").click()
    session_page.get_by_test_id("processing-4001").get_by_placeholder("结束日期").fill(unit["end_date"])
    session_page.get_by_test_id("processing-4001").get_by_placeholder("结束日期").press("Enter")

    if unit["rule_format"] == "文本":
        session_page.get_by_test_id("processing-4002").get_by_text("文本").click()
    else:
        session_page.get_by_test_id("processing-4002").get_by_text("工单").click()

    list1_data_channels = unit["rule_data_channels"].split(",")

    # 选择数据渠道
    if len(list1_data_channels) == 1:
        session_page.get_by_test_id("processing-4003").click()
        session_page.get_by_test_id("processing-4003").locator("path").nth(1).click()
        session_page.get_by_role("menuitem", name=unit["rule_data_channels"]).locator("span div").click()
        session_page.get_by_test_id("processing-4006").get_by_text("内容格式", exact=True).click()
    if len(list1_data_channels) == 2:
        session_page.get_by_test_id("processing-4003").click()
        session_page.get_by_test_id("processing-4003").locator("path").nth(1).click()
        session_page.get_by_role("menu").get_by_text(list1_data_channels[0]).click()
        session_page.get_by_role("menuitem", name=list1_data_channels[1]).locator("span div").click()
        session_page.get_by_test_id("processing-4003").get_by_text("内容格式", exact=True).click()
    if len(list1_data_channels) == 3:
        session_page.get_by_test_id("processing-4003").click()
        session_page.get_by_test_id("processing-4003").locator("path").nth(1).click()
        session_page.get_by_role("menu").get_by_text(list1_data_channels[0]).click()
        session_page.get_by_role("menu").get_by_text(list1_data_channels[1]).click()
        session_page.get_by_role("menuitem", name=list1_data_channels[2]).locator("span div").click()
        session_page.get_by_test_id("processing-4006").get_by_text("内容格式", exact=True).click()

    # 选择规则条件
    # 字段
    for i in range(len(unit["rule_conditions"])):
        session_page.get_by_test_id("sql-1-9001-" + str(i)).click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][i]["condition_field"])

        # 变量值
        session_page.get_by_test_id("sql-1-9002-" + str(i)).click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list', unit["rule_conditions"][i]["condition_value"])

        # 逻辑运算符
        session_page.get_by_test_id("sql-1-9003-" + str(i)).click()
        get_dropdown_values(session_page, '.arco-select-dropdown-list',
                            unit["rule_conditions"][i]["condition_operator"])

        # 条件类型
        if unit["rule_conditions"][i]["condition_operator"] == '为空' or unit["rule_conditions"][i][
            "condition_operator"] == '不为空':
            pass
        else:
            session_page.get_by_test_id("sql-1-9004-" + str(i)).click()
            get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                unit["rule_conditions"][i]["condition_type"])

            # 条件详情
            if unit["rule_conditions"][i]["condition_type"] == '值' or unit["rule_conditions"][i][
                "condition_type"] == '正则表达式':
                session_page.get_by_test_id("sql-1-9005-" + str(i)).locator('input[type="text"]').fill(
                    unit["rule_conditions"][i]["condition_details"])
            else:
                session_page.get_by_test_id("sql-1-9005-" + str(i)).click()
                get_dropdown_values(session_page, '.arco-select-dropdown-list',
                                    unit["rule_conditions"][i]["condition_details"])

        if i < len(unit["rule_conditions"]) - 1:
            session_page.get_by_test_id("sql-1-9007").click()
        else:
            break

    session_page.get_by_test_id("processing-4005").click()
    time.sleep(3)

    session_page.get_by_role("button", name="确定").click()

    session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").click()
    session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").fill(rule_name)
    session_page.get_by_test_id("processing-custom-1008").click()

    test_str = session_page.get_by_test_id("processing-custom-2001-t9-0").inner_text()
    if test_str == '测试中':
        time.sleep(80)
        session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").click()
        session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").fill(rule_name)
        session_page.get_by_test_id("processing-custom-1008").click()
        test_str1 = session_page.get_by_test_id("processing-custom-2001-t9-0").inner_text()

        if test_str1 == '测试成功':
            print("测试成功")
            session_page.get_by_test_id("processing-2010-0").click()
            test_str2 = session_page.locator(
                "td:nth-child(4) > .arco-table-cell > .arco-auto-tooltip > .arco-auto-tooltip-content").first.inner_text()
            print("【test_str2】", test_str2)
            if test_str2 is not None:
                print("验证成功")
                session_page.mouse.move(0, 0)
                # 在当前位置执行点击操作
                session_page.mouse.click(x=0, y=0)
            elif test_str2 is None:
                raise Exception("验证失败:test_str2为空或未找到")

        elif test_str1 == '测试中':
            print("测试中，继续等待")
            time.sleep(30)
            session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").click()
            session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").fill(rule_name)
            session_page.get_by_test_id("processing-custom-1008").click()

            test_str3 = session_page.get_by_test_id("processing-custom-2001-t9-0").inner_text()
            if test_str3 == '测试成功':
                session_page.get_by_test_id("processing-2010-0").click()
                test_str4 = session_page.locator(
                    "td:nth-child(4) > .arco-table-cell > .arco-auto-tooltip > .arco-auto-tooltip-content")
                if test_str4 is not None:
                    print("验证成功")
                    session_page.mouse.move(0, 0)
                    # 在当前位置执行点击操作
                    session_page.mouse.click(x=0, y=0)
                else:
                    raise Exception("测试失败")
                    session_page.mouse.move(0, 0)
                    # 在当前位置执行点击操作
                    session_page.mouse.click(x=0, y=0)
            elif test_str3 == '测试失败' or test_str3 == '测试中':
                raise Exception("测试失败")
    else:
        raise Exception("测试失败")

    session_page.reload()

# def test_click_rule_operation(session_page):
#     session_page.get_by_test_id("processing-1002").click()  # 转入定制规则
#     session_page.locator("tr:nth-child(2) > td:nth-child(12) > .arco-table-cell").first.get_by_test_id(
#         "processing-2006").click()
#
#     session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").click()
#     session_page.get_by_test_id("processing-custom-1001").get_by_placeholder("请输入").fill("规则0323620240524")
#     session_page.get_by_test_id("processing-custom-1008").click()
#
#     test_str = session_page.get_by_test_id("processing-custom-2001-t8-0").inner_text()
#     if test_str == '校验成功':
#         print("校验成功")
#         session_page.get_by_test_id("processing-2009-0").click()
#         test_str1 = session_page.locator("/html/body/div[11]/div[2]/div[2]/div[2]/div/div/div[1]/div/div/table/tbody/tr[1]/td[4]/span/span/span")
#         if test_str1 is not None:
#             print("验证成功")
#         else:
#             raise ValueError("校验失败")
#     else:
#         raise ValueError("校验失败")
